package com.jkc.xh.fragments;

import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;

import com.jkc.xh.R;
import com.jkc.xh.fragments.core.BaseFragment;
import com.jkc.xh.manager.UserManager;
import com.jkc.xh.network.RestCallBack;
import com.jkc.xh.network.models.RestError;
import com.jkc.xh.views.widget.NavBar;
import com.jkc.xh.views.widget.ToastUtil;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.ViewById;

/**
 * Created by zhangbreeze on 15/11/21.
 */
@EFragment(R.layout.fragment_change_pwd)
public class ChangePwdFragment extends BaseFragment {
    @ViewById
    EditText inputOrginPwd;
    @ViewById
    EditText inputPassword;
    @ViewById
    EditText inputPasswordAgain;
    @ViewById(R.id.nav_bar)
    NavBar mNavBar;

    @AfterViews
    void initView() {
        mNavBar.setOnMenuClickListener(new NavBar.OnMenuClickListener() {

            @Override
            public void onLeftMenuClick(View view) {
                finishFragment();
            }
        });
    }

    @Click(R.id.btnSave)
    void savePwd() {
        String orginPwd = inputOrginPwd.getText().toString();
        String newPwd = inputPassword.getText().toString();
        String newPwdAgain = inputPasswordAgain.getText().toString();
        if (TextUtils.isEmpty(orginPwd)) {
            ToastUtil.showToastShort("请输入原密码");
            return;
        }
        if (orginPwd.length() < 6) {
            ToastUtil.showToastShort("原密码至少6位");
            return;
        }
        if (TextUtils.isEmpty(newPwd)) {
            ToastUtil.showToastShort("请输入新密码");
            return;
        }
        if (newPwd.length() < 6) {
            ToastUtil.showToastShort("新密码至少6位");
            return;
        }
        if (TextUtils.isEmpty(newPwdAgain)) {
            ToastUtil.showToastShort("请输入确认密码");
            return;
        }
        if (newPwdAgain.length() < 6) {
            ToastUtil.showToastShort("新密码至少6位");
            return;
        }
        if (!newPwd.equals(newPwdAgain)) {
            ToastUtil.showToastShort("两次输入的密码不一致");
            return;
        }
        showProgress();
        addJob(UserManager.modifyUserPwd(orginPwd, newPwd, new RestCallBack<Object>() {
            @Override
            public void success(Object o) {
                hideProgress();
                ToastUtil.showToastShort("修改成功");
                finishFragment();
            }

            @Override
            public void failure(RestError error) {
                hideProgress();
                ToastUtil.showToastShort(error.msg);
            }
        }));

    }
}
